﻿using HCQ2_Model.VRDD.InputDto._0818;
using HCQ2_Model.VRDD.OutputDto._0818;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace HCQ2_BLL
{
    public partial class T_VRDDCircleCourseRelevanceBLL
    {
        /// <summary>
        ///  获取店铺关联课程列表
        /// </summary>
        /// <param name="store_id">店铺id</param>
        /// <param name="keyword">关键字</param>
        /// <param name="page">第几页</param>
        /// <param name="rows">每页数量</param>
        /// <param name="total">返回总数</param>
        /// <returns></returns>
        public List<OutputCircleRel> GetCircleRelList(int store_id, string keyword, int page, int rows, out int total)
        {
            return DBSession.IT_VRDDCircleCourseRelevanceDAL.GetCircleRelList(store_id, keyword, page, rows, out total);
        }
        /// <summary>
        ///  删除店铺、课程关联
        /// </summary>
        /// <param name="course_id"></param>
        /// <returns></returns>
        public int DeleteCircleRel(string course_id)
        {
            return DBSession.IT_VRDDCircleCourseRelevanceDAL.DeleteCircleRel(course_id);
        }

        /// <summary>
        ///  获取添加关联时的课程列表
        /// </summary>
        /// <param name="store_id">店铺id</param>
        /// <param name="keyword">关键字</param>
        /// <param name="page">第几页</param>
        /// <param name="rows">每页数量</param>
        /// <param name="total">返回总数</param>
        /// <returns></returns>
        public List<OutputCircleRel> GetAddCircleRelList(int store_id, string keyword, int page, int rows, out int total)
        {
            return DBSession.IT_VRDDCircleCourseRelevanceDAL.GetAddCircleRelList(store_id, keyword, page, rows, out total);
        }

        /// <summary>
        ///  获取店铺关联节点
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public List<OutputStoreId> GetStoreCourseList(InputStoreId param)
        {
            List<OutputStoreId> list = new List<OutputStoreId>();
            List<StoreCourseResult> obj = DBSession.IT_VRDDCircleCourseRelevanceDAL.GetStoreCourseList(param);
            if (obj == null || obj.Count <= 0)
                return null;
            List<StoreCourseResult> p = GetFirstTreeData(obj)?.OrderByDescending(s => s.folder_order)?.ToList();
            foreach (StoreCourseResult item in p)
            {
                OutputStoreId temp = item.ToOutputStoreId();
                if (!string.IsNullOrEmpty(temp.Icon))
                    temp.Icon = temp.Icon.Replace("~", HttpContext.Current.Request.Url.AbsoluteUri.Substring(0, HttpContext.Current.Request.Url.AbsoluteUri.IndexOf("/api/")));
                temp.Children = GetModelById(p, item.ID);
                list.Add(temp);
            }
            return list;
        }

        #region 获取第一层数据 T_DocumentFolder
        /// <summary>
        ///  获取第一层数据
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        private List<StoreCourseResult> GetFirstTreeData(List<StoreCourseResult> list)
        {
            List<StoreCourseResult> treeList = new List<StoreCourseResult>();
            if (list == null || list.Count <= 0)
                return null;
            treeList = list.FindAll(s => s.folder_pid == 0);
            list.ForEach(o =>
            {
                var obj = GetSecondTreeData(list, o, o.folder_pid);
                if (obj.folder_pid != 0)
                {
                    var ss = treeList.Where(s => s.ID == obj.ID);
                    if (ss == null || ss.Count() <= 0)
                        treeList.Add(obj);
                }
            });
            return treeList;
        }
        private StoreCourseResult GetSecondTreeData(List<StoreCourseResult> list, StoreCourseResult folder, int folder_pid)
        {
            var query = list.Where(o => o.ID == folder_pid)?.FirstOrDefault();
            if (query != null)
                return GetSecondTreeData(list, query, query.folder_pid);
            return folder;
        }
        #endregion
        /// <summary>
        ///  递归获取文档树
        /// </summary>
        /// <param name="list"></param>
        /// <param name="folder_id"></param>
        /// <returns></returns>
        private List<OutputStoreId> GetModelById(List<StoreCourseResult> list, int folder_id)
        {
            List<OutputStoreId> listModel = new List<OutputStoreId>();
            List<StoreCourseResult> listKey = list.FindAll(s => s.folder_pid == folder_id)?.OrderByDescending(s => s.folder_order)?.ToList();
            if (listKey.Count > 0)
            {
                foreach (StoreCourseResult item in listKey)
                {
                    OutputStoreId temp = item.ToOutputStoreId();
                    if (!string.IsNullOrEmpty(temp.Icon))
                        temp.Icon = temp.Icon.Replace("~", HttpContext.Current.Request.Url.AbsoluteUri.Substring(0, HttpContext.Current.Request.Url.AbsoluteUri.IndexOf("/api/")));
                    temp.Children = GetModelById(list, item.ID);
                    list.Add(temp);
                }
            }
            else
                return null;
            return listModel;
        }
    }
}